package cn.edu.zjgsu.ito.controller;
import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import cn.edu.zjgsu.ito.service.AdminExportStuInfService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

@Before(AdminControllerInterceptor.class)
public class AdminExportStuInfController extends Controller{
	private List<Record> records;
	private Record record;
	private AdminExportStuInfService adminExportStuInfService =ServiceFactory.createAdminExportStuInfService();
	public static final DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-HH：mm");
	//学生信息导出
		public void exportExcel() throws FileNotFoundException
		{
			Date d=new java.util.Date();
			String filedir=PathKit.getWebRootPath();
	 		filedir=filedir+"\\WEB-INF\\download\\";
	 		
	        deleteFile(new File(filedir));//清空缓存区
	         
	 		String firstName;
	 		
	 		
	 		records=adminExportStuInfService.Studentnews();
	 		
	 		firstName=records.get(0).get("firstName");
	 		firstName=firstName.replace(" ", "-");
	 		firstName=firstName+"学生信息汇总表("+formatter.format(d)+").xls";
	 		
	 		OutputStream out = new FileOutputStream(filedir+firstName);	 		
	 		System.out.println(filedir+firstName);
	 		adminExportStuInfService.exportExcel(records, out);
	 		String outpath=filedir+firstName;
	 		
	 		renderFile(new File(outpath));
		}
		 public void deleteFile(File oldPath) {
	          if (oldPath.isDirectory()) {
	           File[] files = oldPath.listFiles();
	           for (File file : files) {
	             deleteFile(file);
	           }
	          }else{
	            oldPath.delete();
	          }
	        }
}
